﻿// CherryMerry ( Mikhail Tomilenko ), Reverse Entertainment, 2021-2024


#include "../Public/EnemyHealthManager.h"

#include <Kismet/GameplayStatics.h>
#include "Runtime/Engine/Public/TimerManager.h"


UEnemyHealthManager::UEnemyHealthManager()
{
	PrimaryComponentTick.bCanEverTick = false;

	bCanDamage = true;
}


void UEnemyHealthManager::BeginPlay()
{
	Super::BeginPlay();

	AActor* Owner = GetOwner();

	if (Owner)
	{
		UE_LOG(LogTemp, Warning, TEXT("Owner is valid"))
		Owner->OnTakeAnyDamage.AddDynamic(this, &UEnemyHealthManager::TakeDamage);
	}

}

void UEnemyHealthManager::TakeDamage(AActor* DamagedActor, float Damage, const UDamageType* DamageType, AController* InstigatedBy, AActor* DamageCauser)
{
	if (!bIsDead)
	{
		if (bCanDamage) /* && CombatManager->bBlockingDamage == false (вернуть после создания менеджера боёвки) */
		{
			if (Damage <= 0)
			{
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Damage <= 0")));
				return;
			}
			else
			{
				GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("Damage > 0 \nDamage: %f"), Damage));
				if (CurrentShield > 0)
				{
					if (Damage >= CurrentShield)
					{
						CurrentShield = 0;
						GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("CurrentShield: %f"), CurrentShield));
					}
					else
					{
						CurrentShield = FMath::Clamp(CurrentShield - Damage, 0.0f, MaxShield);
						GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Blue, FString::Printf(TEXT("CurrentShield: %f"), CurrentShield));
					}
				}
				else if (CurrentHealth > 0)
				{
					if (Damage >= CurrentHealth)
					{
						CurrentHealth = 0;
						GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red, FString::Printf(TEXT("CurrentHealth: %f\nYou dead :("), CurrentHealth));
						Death();
					}
					else
					{
						CurrentHealth = FMath::Clamp(CurrentHealth - Damage, 0.0f, MaxHealth);;
						GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Green, FString::Printf(TEXT("CurrentHealth: %f"), CurrentHealth));
					}
				}
			}
		}
		/*else if (!bCanDamage && CombatManager->bBlockingDamage == true)
		{
			MovementManager->UseStamina(25);
			if (MovementManager->CurrentStamina <= 0)
			{
				CombatManager->StopBlockIncomingDamage();
			}
		} (вернуть после создания менеджера боёвки) */
	}
}

void UEnemyHealthManager::Death()
{
	AActor* Owner = GetOwner();
	UE_LOG(LogTemp, Warning, TEXT("Death"));

	if (Owner)
	{
		Owner->DisableInput(UGameplayStatics::GetPlayerController(GetWorld(), 0));

		GetWorld()->GetTimerManager().SetTimer(DeathTimerHandle, this, &UEnemyHealthManager::AfterDeath, 5.f, false);
	}
}

void UEnemyHealthManager::AfterDeath()
{
	AActor* Owner = GetOwner();

	if (Owner)
	{
		GetWorld()->GetTimerManager().ClearTimer(DeathTimerHandle);
		Owner->Destroy();
	}
}

void UEnemyHealthManager::TickComponent(float DeltaTime, ELevelTick TickType, FActorComponentTickFunction* ThisTickFunction)
{
	Super::TickComponent(DeltaTime, TickType, ThisTickFunction);

}

